package perf.store;

import java.util.List;

import perf.data.Run;
import perf.data.Setup;

import com.db4o.ObjectContainer;

public class ObjectModify {
	private ObjectContainer store;
	private ObjectList lister;
	
	public ObjectModify( ObjectContainer db, ObjectList lister ) {
		this.store = db;
		this.lister = lister;
	}
	
	public void addSetup( Setup setup ) {
		store.store( setup );
		store.commit();
	}
	
	public void deleteSetup( int idx ) {
		List<Setup> setups = lister.setups();
		Setup s = setups.get( idx );
		store.delete( s );
		store.commit();
	}
	
	public void deleteRun( int setupIdx, int runIdx ) {
		List<Run> list = lister.setup( setupIdx ).siblings();
		if ( runIdx >= list.size() ) {
			throw new RuntimeException( "Invalid run index: " + runIdx );
		} else {
			list.remove(runIdx);
		}
	}
	
	public void addRun( int setupIdx, Run run ) {

		Setup s = lister.setup( setupIdx );
		s.siblings().add( run );
		store.store( s );
		store.commit();
	}
}

